package com.china.produce.controller;

import com.china.produce.enums.ProductTypeEnum;
import com.china.produce.plugins.ProductObjPlugins;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.SimplePluginRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2025/7/23
 */
@RequestMapping("/product")
@RestController
public class ProductController {

    private final List<ProductObjPlugins> plugins;

    private final PluginRegistry<ProductObjPlugins, ProductTypeEnum> pluginRegistry;

    public ProductController(List<ProductObjPlugins> plugins) {
        this.plugins = plugins;
        this.pluginRegistry = SimplePluginRegistry.create(plugins);
    }

    @GetMapping("/find")
    public String processProduct(@RequestParam String code, @RequestParam Long productId) {

        ProductTypeEnum type = ProductTypeEnum.getByCode(code);

        ProductObjPlugins plugin = pluginRegistry.getPluginFor(type);

        return plugin.findProduct(productId);
    }
}
